#pragma once

#include "ObjBase.hpp"
#include <QByteArray>
#include <QMetaObject>
#include <QHash>


class ObjectFactory
{
public:
    template<typename T>
    static void registerClass()
    {
        //QString name = T::staticMetaObject.className();
        constructors().insert( T::staticMetaObject.className(), &constructorHelper<T> );
    }

    static ObjBase* createObject( const QByteArray& className, ObjBase* parent = nullptr )
    {
        Constructor constructor = constructors().value( className );
        if ( !constructor)
            return nullptr;
        return (*constructor)( parent );
    }

private:
    typedef ObjBase* (*Constructor)(ObjBase* parent );

    template<typename T>
    static ObjBase* constructorHelper(ObjBase* parent )
    {
        return new T( parent );
    }

    static QHash<QByteArray, Constructor>& constructors()
    {
        static QHash<QByteArray, Constructor> instance;
        return instance;
    }
};